<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 * 订单列表api
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ReservetimeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/AppointCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceinfoCom.php");

$log = new Logger("./../orderlistapi.log");
$common = new Common();
$ordercom = new OrderCom();
$reservetimecom = new ReservetimeCom();
$appointcom = new AppointCom();
$serviceinfocom = new ServiceinfoCom();

if (!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) {
    echo json_encode(array("code" => 100, "msg" => "cannot find userid"));
    exit();
}

$userid = $_REQUEST['userid'];
//测试数据
//$userid = 1;

$orderlistarr = $ordercom->getOrderlist($userid);
if (!$orderlistarr) {
    echo json_encode(array("code" => 400, "msg" => "get orderlist failed"));
    exit();
}

$result = array("code" => 0, "msg" => "success", "data" => array());
foreach ($orderlistarr as $k => $v) {
    $result["data"][$k]['id'] = $v['id'];
    $result["data"][$k]['createtime'] = date("Y-m-d",$v['create_time']);
    $result["data"][$k]["totalfee"] = sprintf("%.2f", $v['totalfee'] / 100);
    $result["data"][$k]["tradeno"] = $v['trade_no'];
    if ($v['is_pay'] == 0) {
        $result["data"][$k]["status"] = "未支付";
    } else {
        $result["data"][$k]["status"] = "已支付";
    }

    $result["data"][$k]["ispay"] = $v['is_pay'];
//    $a = $reservetimecom->getRtimeById($v['reservetime_id']);
//    $time = date("m-d", $v['time']);
//    $result["data"][$k]["reservetime"] = $time . " " . $a['info'];
//    $appoint = $appointcom->getAppointDesc($v['id']);
//    if (!$appoint) {
//        echo json_encode(array("code" => 400, "msg" => "get appoint failed"));
//        exit();
//    }
//    $aa = $reservetimecom->getRtimeById($appoint['reservetime_id']);
//    $time = date("m-d", $appoint['time']);
//    $result["data"][$k]["reservetime"] = $time . " " . $a['info'];
}

$resjson = json_encode($result);
$log->debug("result : " . $resjson);
echo $resjson;
exit();
